package com.rfid.afd.model;

import android.text.TextUtils;

import com.rfid.config.ConfigManager;
import com.rfid.db.bean.Rfid;
import com.rfid.db.bean.ShunjiaLevel;
import com.rfid.jni.RfidDecoder;
import com.rfid.library.zhongshan.BookIndex;
import com.rfid.util.DBManager;
import com.rfid.util.LogUtils;
import com.rfid.util.UserManager;
import com.rfid.util.Util;

import java.util.ArrayList;
import java.util.List;

public class ShunjiaModel extends BaseModel{


    private RfidDecoder mRfidDecoder;
    boolean hasShelf;
    private String mShelfEpc;

    public ShunjiaModel() {
        mRfidDecoder = new RfidDecoder();
    }

    public List<Rfid> getRfidsByIndexRanger(Rfid startIndex, Rfid endIndex) {
        List<Rfid> data = new ArrayList<>();
        String like = getSameStringFromStart(startIndex.getRindex(),endIndex.getRindex());
        if(TextUtils.isEmpty(like)) {
            LogUtils.w("getRfidsByIndexRanger fail,No same string");
            return data;
        }
        if(like.length() > 3) {
            like = like.substring(0,3);
        }
        List<Rfid> rfids = DBManager.queryRfidsLikeIndex(like);
        LogUtils.v("rfids size = " + rfids.size() + ",like = " + like);
        for(Rfid rfid:rfids) {
            BookIndex bookIndex = new BookIndex(rfid.getRindex(),rfid.getBookName(),rfid.getSite(),"","");
            BookIndex start = new BookIndex(startIndex.getRindex(),startIndex.getBookName(),startIndex.getSite(),"","");
            BookIndex end = new BookIndex(endIndex.getRindex(),endIndex.getBookName(),endIndex.getSite(),"","");
            if(bookIndex.CompareTo(start) >= 0 && bookIndex.CompareTo(end) < 0) {
                data.add(rfid);
            }
        }
        return data;
    }

    private String getSameStringFromStart(String a,String b) {
        StringBuffer s = new StringBuffer();
        for(int i = 0;i<a.length();i++) {
            if(a.charAt(i) == b.charAt(i)) {
                s.append(a.charAt(i));
            } else {
                break;
            }
        }
        return s.toString();
    }

    @Override
    public synchronized void onRfid(String rfid) {
        LogUtils.v("hex = " +rfid + ",hasShelf = " + hasShelf);
        if(hasShelf) {
            super.onRfid(rfid);
            return;
        } else {
            System.out.println("hasShelf = " + hasShelf);
        }
        try {
            String hex = rfid.substring(4 * 2, rfid.length());
            byte data[] = Util.hexStr2bytes(hex);
            int r = 0;
            System.out.println("data len = " + data.length);
            if (hex.length() > 16) {
                System.out.println("iRFID_Decode128bit");
                r = mRfidDecoder.iRFID_Decode128bit(data);
            } else {
                System.out.println("iRFID_Decode96bit");
                r = mRfidDecoder.iRFID_Decode96bit(data);
            }
            String barcode = new String(data).trim();
            if(barcode.length() == ConfigManager.getInstance().getConfig().getShelfLabLen()) {
                mShelfEpc = rfid;
                hasShelf = true;
                barcode = "shelf" + barcode;
            }
            if (r > 0) {
                super.onRfid(barcode);
            } else {
                LogUtils.d("decode fail rfid = " + rfid + ",r = " + r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void shunjia(String shelf, List<Rfid> rfids) {
        for(Rfid rfid : rfids) {
            rfid.setShelf(shelf);
        }
        DBManager.updateRfids(rfids);
    }

    public void addShunjiaLevel(String startIndex,String endIndex,String shelf) {
        ShunjiaLevel level = new ShunjiaLevel();
        level.setStartIndex(startIndex);
        level.setEndIndex(endIndex);
        level.setUid(mShelfEpc);
        level.setShelf(shelf);
        level.setUser(UserManager.getInstance().getLoginUser().getName());
        level.setPlaceName(shelf);
        level.setOptime(Util.getDateTime());
        level.setLibrary("");
        DBManager.addShunjiaLevel(level);
    }
}
